package com.chuji.crmcj.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.SaleNoticeDatailInfo;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.util.ArrayList;
import java.util.List;

public class MagSendNoticeDetailsActivity extends Activity {
    /*回退*/
    private ImageView mIv_Back;
    //头像
    private com.chuji.crmcj.view.CircleImageView mIv_client_image;
    //姓名
    private TextView mTv_name;
    //职位
    private TextView mTv_post;
    //通知标题
    private TextView mTv_notice_title;
    //通知时间
    private TextView mTv_notice_time;
    //通知内容
    private TextView mTv_notice_content;
    //数据存储
    List<SaleNoticeDatailInfo.ApiParamObjEntity> saleNoticeDatailData;
    //网络数据
    private SaleNoticeDatailInfo saleNoticeDatailInfo;
    //意图
    Intent intent;
    //消息ID
    String MessageID;
    //用户ID
    String UserID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mag_send_notice_details);
        mIv_Back= (ImageView) findViewById(R.id.iv_Back);
        mIv_client_image = (com.chuji.crmcj.view.CircleImageView) findViewById(R.id.iv_client_image);
        mTv_name = (TextView) findViewById(R.id.tv_name);
        mTv_post = (TextView) findViewById(R.id.tv_post);
        mTv_notice_title = (TextView) findViewById(R.id.tv_notice_title);
        mTv_notice_time = (TextView) findViewById(R.id.tv_notice_time);
        mTv_notice_content = (TextView) findViewById(R.id.tv_notice_content);
        //回退
        mIv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        initDa();
    }
    public void initDa() {
        /**
         * 获得评价记录
         */
        getNoticeData();
    }
    private void getNoticeData() {
        //第一次
        if (saleNoticeDatailData == null) {
            saleNoticeDatailData = new ArrayList<SaleNoticeDatailInfo.ApiParamObjEntity>();
        }
        requestData();
    }
    private void requestData() {
        //初始化Volley
        UserID= SPUtils.getString(UIUtils.getContext(), "UserID", "");
        intent=getIntent();
        MessageID=intent.getStringExtra("MessageID");
        String strURL = String.format(UrlUtils.SALES_NOTICE_DETAIL+"&UserID="+UserID+"&defeatType=1"+"&MessageID="+MessageID);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                saleNoticeDatailInfo = JSON.parseObject(response, SaleNoticeDatailInfo.class);
                saleNoticeDatailData.addAll(saleNoticeDatailInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        if(saleNoticeDatailData.size()!=0) {
                            mTv_name.setText(saleNoticeDatailData.get(0).getRealName());
                            mTv_post.setText(saleNoticeDatailData.get(0).getPositionName());
                            mTv_notice_title.setText(saleNoticeDatailData.get(0).getTitle());
                            mTv_notice_time.setText(saleNoticeDatailData.get(0).getSendTime());
                            mTv_notice_content.setText(saleNoticeDatailData.get(0).getContent());
                            if(saleNoticeDatailData.get(0).getAvatar()==null){
                                mIv_client_image.setImageResource(R.drawable.morentouxiang);
                            }else {
                                Glide.with(UIUtils.getContext())
                                        .load(UrlUtils.SALES_NOTICE_DETAIL + saleNoticeDatailData.get(0).getAvatar())
                                        .into(mIv_client_image);
                            }
                        }else{
                            UIUtils.showToastSafe("暂无数据");
                        }
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }
}
